package org.csu.molediary.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.molediary.api.common.CommonResponse;
import org.csu.molediary.api.entity.Account;
import org.csu.molediary.api.entity.Diary;
import org.csu.molediary.api.entity.Good;
import org.csu.molediary.api.entity.Tag;
import org.csu.molediary.api.persistence.AccountMapper;
import org.csu.molediary.api.persistence.DiaryMapper;
import org.csu.molediary.api.persistence.GoodMapper;
import org.csu.molediary.api.persistence.TagMapper;
import org.csu.molediary.api.service.DiaryService;
import org.csu.molediary.api.vo.AccountId;
import org.csu.molediary.api.vo.DiaryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service("DiaryService")
public class DiaryServiceImpl implements DiaryService {

    @Autowired
    private DiaryMapper diaryMapper;

    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private GoodMapper goodMapper;

    @Override
    //得到全部的日记
    public CommonResponse<List<DiaryVO>> getDiaryList() {
        List<Diary> diaryList = diaryMapper.selectList(null);
        List<DiaryVO> diaryVOList = new ArrayList<>();

        //为DiaryVOList插入Diary对象
        Iterator<Diary> diaryIterator = diaryList.iterator();
        while(diaryIterator.hasNext()) {
            Diary diary = diaryIterator.next();
            DiaryVO diaryVO = new DiaryVO();

            diaryVO.setDiary(diary);
            QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
            accountQueryWrapper.eq("id",diary.getAccountid());
            Account account = accountMapper.selectOne(accountQueryWrapper);

            diaryVO.setHead(account.getHead());
            diaryVO.setName(account.getName());

            QueryWrapper<Tag> tagQueryWrapper = new QueryWrapper<>();
            tagQueryWrapper.eq("tagid",diary.getTagid());
            Tag tag = tagMapper.selectOne(tagQueryWrapper);
            diaryVO.setTagcontent(tag.getTagcontent());

            diaryVOList.add(diaryVO);
        }
        if(diaryVOList.isEmpty()){
            return CommonResponse.createForSuccessMessage("没有日记");
        }
        return CommonResponse.createForSuccess("查询所有的日记成功",diaryVOList);
    }

    @Override
    //根据日记id来得到某个日记
    public CommonResponse<DiaryVO> getDiaryByContentId(int contentid) {
        QueryWrapper<Diary> diaryQueryWrapper = new QueryWrapper<>();
        diaryQueryWrapper.eq("contentid",contentid);
        Diary diary = diaryMapper.selectOne(diaryQueryWrapper);
        System.out.println(diary);
        if(diary==null){
            return CommonResponse.createForSuccessMessage("没有该id的日记");
        }
        DiaryVO diaryVO = new DiaryVO();

        diaryVO.setDiary(diary);

        QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
        accountQueryWrapper.eq("id",diary.getAccountid());
        Account account = accountMapper.selectOne(accountQueryWrapper);

        diaryVO.setHead(account.getHead());
        diaryVO.setName(account.getName());

        QueryWrapper<Tag> tagQueryWrapper = new QueryWrapper<>();
        tagQueryWrapper.eq("tagid",diary.getTagid());
        Tag tag = tagMapper.selectOne(tagQueryWrapper);
        diaryVO.setTagcontent(tag.getTagcontent());

        return  CommonResponse.createForSuccess("查询某日记成功",diaryVO);
    }

    @Override
    //根据用户id来得到日记
    public CommonResponse<List<DiaryVO>> getDiaryByAccountId(String accountid) {
        QueryWrapper<Diary> diaryQueryWrapper = new QueryWrapper<>();
        diaryQueryWrapper.eq("accountid",accountid);
        List<Diary> diaryList = diaryMapper.selectList(diaryQueryWrapper);

        List<DiaryVO> diaryVOList = new ArrayList<>();

        //为DiaryVOList插入Diary对象
        Iterator<Diary> diaryIterator = diaryList.iterator();
        while(diaryIterator.hasNext()) {
            Diary diary = diaryIterator.next();
            DiaryVO diaryVO = new DiaryVO();

            diaryVO.setDiary(diary);
            QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
            accountQueryWrapper.eq("id",diary.getAccountid());
            Account account = accountMapper.selectOne(accountQueryWrapper);

            diaryVO.setHead(account.getHead());
            diaryVO.setName(account.getName());

            QueryWrapper<Tag> tagQueryWrapper = new QueryWrapper<>();
            tagQueryWrapper.eq("tagid",diary.getTagid());
            Tag tag = tagMapper.selectOne(tagQueryWrapper);
            diaryVO.setTagcontent(tag.getTagcontent());

            diaryVOList.add(diaryVO);
        }
        if(diaryVOList.isEmpty()){
            return CommonResponse.createForError("获取异常");
        }
        return CommonResponse.createForSuccess("查询某用户的所有日记成功",diaryVOList);
    }

    @Override
    //删除某篇日记
    public CommonResponse<String> deleteDiaryByContentId(int contentid) {
        QueryWrapper<Diary> diaryQueryWrapper = new QueryWrapper<>();
        diaryQueryWrapper.eq("contentid",contentid);
        Diary diary = diaryMapper.selectOne(diaryQueryWrapper);
        if(diary==null){
            return CommonResponse.createForError("没有对应该id的日记，故删除无效");
        }
        diaryMapper.delete(diaryQueryWrapper);
        return CommonResponse.createForSuccessMessage("删除成功");
    }

    @Override
    //新建日记
    public CommonResponse<Diary> createDiary(String content, String title, String accountid, int tagid, String province,
                                             String distinct, String city, String photo1, String photo2, String photo3,String diarydate) {

        Diary diary = new Diary();
        diary.setAccountid(accountid);
        diary.setCity(city);
        diary.setContent(content);
        diary.setDistinct1(distinct);
        diary.setProvince(province);
        diary.setTagid(tagid);
        diary.setPhoto1(photo1);
        diary.setPhoto2(photo2);
        diary.setPhoto3(photo3);
        diary.setTitle(title);
        diary.setGoodnum(0);
        diary.setDiarydate(diarydate);

        int i = diaryMapper.insert(diary);

        if(i<1){
            return CommonResponse.createForError("新建日记失败");
        }
        return CommonResponse.createForSuccess("新建日记成功",diary);
    }

    @Override
    //通过日记id获取用户id
    public CommonResponse<AccountId> getAccountIdByDiaryId(int contentid) {
        Diary diary = diaryMapper.selectById(contentid);
        if(diary==null){
            return CommonResponse.createForError("通过日记id获取用户id错误");
        }
        AccountId accountId = new AccountId();
        accountId.setAccountid(diary.getAccountid());
        return CommonResponse.createForSuccess("用户id查找成功",accountId);
    }

    @Override
    //通过模糊查询标题来查询日记
    public CommonResponse<List<DiaryVO>> getDiaryByTitle(String tit) {
        QueryWrapper<Diary> diaryQueryWrapper = new QueryWrapper<>();
        diaryQueryWrapper.like("title",tit);
        List<Diary> diaryList = diaryMapper.selectList(diaryQueryWrapper);

        List<DiaryVO> diaryVOList = new ArrayList<>();

        //为DiaryVOList插入Diary对象
        Iterator<Diary> diaryIterator = diaryList.iterator();
        while(diaryIterator.hasNext()) {
            Diary diary = diaryIterator.next();
            DiaryVO diaryVO = new DiaryVO();

            diaryVO.setDiary(diary);
            QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
            accountQueryWrapper.eq("id",diary.getAccountid());
            Account account = accountMapper.selectOne(accountQueryWrapper);

            diaryVO.setHead(account.getHead());
            diaryVO.setName(account.getName());

            QueryWrapper<Tag> tagQueryWrapper = new QueryWrapper<>();
            tagQueryWrapper.eq("tagid",diary.getTagid());
            Tag tag = tagMapper.selectOne(tagQueryWrapper);
            diaryVO.setTagcontent(tag.getTagcontent());

            diaryVOList.add(diaryVO);
        }

        if(diaryVOList.isEmpty()){
            return CommonResponse.createForSuccessMessage("没有标题中含有此内容的日记");
        }
        return CommonResponse.createForSuccess("通过模糊查询标题来查询日记成功",diaryVOList);
    }

    @Override
    //根据用户id获取用户点赞的所有日记信息
    public CommonResponse<List<DiaryVO>> getDiaryByGood(String accountid) {
        QueryWrapper<Good> goodQueryWrapper=new QueryWrapper<>();
        goodQueryWrapper.eq("goodone",accountid);
        List<Good> goodList=goodMapper.selectList(goodQueryWrapper);

        List<DiaryVO> diaryVOList = new ArrayList<>();

        //为DiaryVOList插入Diary对象
        Iterator<Good> goodIterator = goodList.iterator();
        while(goodIterator.hasNext()) {
            Good good = goodIterator.next();
            QueryWrapper<Diary> diaryQueryWrapper = new QueryWrapper<>();
            diaryQueryWrapper.eq("contentid",good.getDiaryid());
            Diary diary = diaryMapper.selectOne(diaryQueryWrapper);
            DiaryVO diaryVO = new DiaryVO();

            diaryVO.setDiary(diary);
            QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
            accountQueryWrapper.eq("id",diary.getAccountid());
            Account account = accountMapper.selectOne(accountQueryWrapper);

            diaryVO.setHead(account.getHead());
            diaryVO.setName(account.getName());

            QueryWrapper<Tag> tagQueryWrapper = new QueryWrapper<>();
            tagQueryWrapper.eq("tagid",diary.getTagid());
            Tag tag = tagMapper.selectOne(tagQueryWrapper);
            diaryVO.setTagcontent(tag.getTagcontent());

            diaryVOList.add(diaryVO);
        }

        if(diaryVOList.isEmpty()){
            return CommonResponse.createForSuccessMessage("用户没有点赞的日记");
        }
        return CommonResponse.createForSuccess("根据用户id获取用户点赞的所有日记信息",diaryVOList);
    }

    @Override
    public CommonResponse<Diary> addNumDiary(int diaryid) {
        QueryWrapper<Diary> diaryQueryWrapper=new QueryWrapper<>();
        diaryQueryWrapper.eq("contentid",diaryid);
        Diary diary=diaryMapper.selectOne(diaryQueryWrapper);
        diary.setGoodnum(diary.getGoodnum()+1);
        diaryMapper.update(diary,diaryQueryWrapper);
        return CommonResponse.createForSuccess(diary);
    }
}
